package com.my.zxsyd.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.my.zxsyd.entity.Xygl;
import com.my.zxsyd.entity.Zworder;
import com.my.zxsyd.service.IXyglService;
import com.my.zxsyd.service.IZworderService;
import com.my.zxsyd.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 定时任务检测15分钟未支付的订单直接取消
 */
@Component
public class ZwOrderTask {

    @Autowired
    private IZworderService iZworderService;

    @Autowired
    private IXyglService iXyglService;

    @Scheduled(cron = "0/1 * * * * ?")
    public void orderTask(){
        QueryWrapper<Zworder> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("paystatus",1);
        queryWrapper.eq("status",1);
        List<Zworder> zworderList=this.iZworderService.list(queryWrapper);
        for (Zworder zworder : zworderList) {
            BigDecimal month= DateUtils.checkMinDate(zworder.getCtime(),new Date());
            if(month.compareTo(BigDecimal.valueOf(15L))==1){//大于了15
                zworder.setStatus(2);//系统自动取消
                this.iZworderService.updateById(zworder);
                //增加当前用户违约
                Xygl xygl=new Xygl();
                xygl.setYhid(zworder.getYhid());
                xygl.setCtime(new Date());
                xygl.setJfs(BigDecimal.valueOf(10));
                xygl.setContent("恶意预约座位15分钟未支付，系统自动取消");
                this.iXyglService.save(xygl);
            }
        }
    }

    public static void main(String[] args) throws ParseException {
        Date str=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2024-02-21 08:53:57");
        BigDecimal month= DateUtils.checkMinDate(str,new Date());
        if(month.compareTo(BigDecimal.valueOf(15L))==1){//大于了15
            System.out.println("大于"+month);
        }else{
            System.out.println("小于"+month);
        }

    }



}
